package com.wushi.lenovo.asia5b.app_ui.orderdetail;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_model.good.OrderLogistic;
import com.wushi.lenovo.asia5b.app_model.good.goodbean.OrderGoodBean;
import com.wushi.lenovo.asia5b.app_model.other.UserInfo;
import com.wushi.lenovo.asia5b.app_ui.other.dialog.CancelSureDialog;
import com.wushi.lenovo.asia5b.app_widget.LogisticsView2;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

import static timber.log.Timber.d;

/**
 * Created by zhangyuncai on 2020/3/12.
 */
public class OrderDetailDeliveryFragment extends OrderDetailFragment {


    @BindView(R.id.tv_orderdetail_orderno_text)
    TextView tv_orderdetail_orderno_text;
    @BindView(R.id.tv_orderdetail_no)
    TextView tv_orderdetail_no;
    @BindView(R.id.tv_orderdetail_time)
    TextView tv_orderdetail_time;
    @BindView(R.id.tv_orderdetail_billingaddress_content)
    TextView tv_orderdetail_billingaddress_content;
    @BindView(R.id.logistics_orderdetail)
    LogisticsView2 logistics_orderdetail;
    @BindView(R.id.bt_orderdetail_trackint)
    Button bt_orderdetail_trackint;
    @BindView(R.id.rv_orderdetail_goods)
    RecyclerView rv_orderdetail_goods;
    @BindView(R.id.tv_order_button)
    TextView tv_order_button;
    @BindView(R.id.rv_orderdetail_price)
    RecyclerView pricedetailRecyclerView;
    @BindView(R.id.tv_orderdetail_viewiransaction)
    TextView tv_orderdetail_viewiransaction;
    @BindView(R.id.tv_ordercheck_totalprice)
    TextView tv_ordercheck_totalprice;
    @BindView(R.id.ll_ordercheck_totalprice_vessel)
    LinearLayout ll_ordercheck_totalprice_vessel;

    @Override
    protected int getLayout() {
        return R.layout.fragment_orderdetail_delivery;
    }

    @Override
    protected void initView() {
        super.initView();
        tv_order_button.setVisibility(View.GONE);
    }

    public CancelSureDialog getCancelSureDialog(String titleContent, String leftContent, String rightContent) {
        CancelSureDialog dialog = new CancelSureDialog.Builder(getContext())
                .setContent(titleContent)
                .setLeftContent(leftContent)
                .setRightContent(rightContent)
                .build();
        return dialog;
    }

    private void receive(OrderGoodBean.ActionBean actionBean) {
        if (actionBean == null) {
            return;
        }
        String actionCode = actionBean.getCode();
        if (TextUtils.isEmpty(actionCode)) {
            d("--->>>>>>>>---actionCode.isEmpty");
            return;
        }
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("member_id", UserInfo.getMemberId());
        OrderGoodBean.Popup_buttonBean popup_button_left = actionBean.getPopup_button_left();
        Map<String, String> leftButtonParam = popup_button_left.getParam();//当点击左边button中里面的参数

        CancelSureDialog dialog = getCancelSureDialog(actionBean.getPopup_content(), popup_button_left.getName()
                , actionBean.getPopup_button_right().getName());
        dialog.setCallBack((s) -> {
            if (leftButtonParam != null) {
                paramMap.putAll(leftButtonParam);
            }
            getPresenter().button_order(paramMap, popup_button_left.getAjaxUrlSuffix());
        });
        dialog.show();
    }

    @Override
    protected void setOrderDetailView() {
        if (model != null) {
            /*本地派送信息*/
            //订单号 tv_orderdetail_no
            tv_orderdetail_no.setText(model.getOrder_no());
            if (TextUtils.isEmpty(model.getOrder_no())) {
                tv_orderdetail_no.setText("-");
            }
            //订单时间 tv_orderdetail_time
            tv_orderdetail_time.setText(model.getOrder_date());

            //账单地址
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(String.format("%s:", getString(R.string.shippingaddress)));
            stringBuilder.append("\n");

            //拼接地址信息
            contactAddress(stringBuilder, model.getDelivery_address());

            stringBuilder.append("\n\n");
            stringBuilder.append(String.format("%s:", getString(R.string.billingaddress)));
            stringBuilder.append("\n");
            //拼接地址信息
            contactAddress(stringBuilder, model.getBilling_address());

            stringBuilder.append("\n\n");
            stringBuilder.append(String.format("%s:", getString(R.string.paymentmethod)));
            stringBuilder.append("\n");
            if (!TextUtils.isEmpty(model.getPayment_method())) {
                stringBuilder.append(model.getPayment_method());
                stringBuilder.append("\n\n");
            }

            stringBuilder.append(String.format("%s:", getString(R.string.internationalshippingmethod)));
            stringBuilder.append("\n");
            if (!TextUtils.isEmpty(model.getInternational_shipping_method())) {
                stringBuilder.append(model.getInternational_shipping_method());
                stringBuilder.append("\n\n");
            }
            stringBuilder.append(String.format("%s:", getString(R.string.addonservices)));
            if (!TextUtils.isEmpty(model.getAdd_on_services())) {
                stringBuilder.append("\n");
                stringBuilder.append(model.getAdd_on_services());
            }

            //物流状态
            bt_orderdetail_trackint.setVisibility(model.show_order_track == 1 ? View.VISIBLE : View.GONE);

            tv_orderdetail_billingaddress_content.setText(stringBuilder.toString());


            /*商品列表*/
            setGoodsView(model.getOrder_parcel(), rv_orderdetail_goods);


            //价格列表
            setPriceView(pricedetailRecyclerView,ll_ordercheck_totalprice_vessel,tv_ordercheck_totalprice);

            //设置账单下载地址
            setViewTransactionView(tv_orderdetail_viewiransaction);


            //按键
            if (model.getOrder_action() != null) {
                OrderGoodBean.ActionBean order_action = model.getOrder_action();
                tv_order_button.setVisibility(View.VISIBLE);
                tv_order_button.setSelected(true);
                tv_order_button.setText(order_action.getName());
                tv_order_button.setOnClickListener(v -> {
                    receive(order_action);
                });
            } else {
                tv_order_button.setVisibility(View.GONE);
            }


        } else {
        }
    }

    @Override
    protected void initData() {
        super.initData();
        getPresenter().getLogisticStatus(order_id, orderLogisticResultModel -> {
            if (orderLogisticResultModel.isSuccess()) {
                OrderLogistic data = orderLogisticResultModel.getData();
                if (data != null) {
                    int status = data.getStatus();
                    logistics_orderdetail.setDeliveredStatus(status);
                }
            }
        });
    }

    @OnClick({
            R.id.bt_orderdetail_trackint
    })
    @Override
    public void onClick(View v) {
        if (order_id <= 0) {
            return;
        }
        switch (v.getId()) {
            case R.id.bt_orderdetail_trackint: {
                if (model != null) {
                    getPresenter().getOrderTrackData(model.getOrder_id());
                }
            }
            break;
        }
    }
}
